package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.pojo.Member;
import com.itheima.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = MemberService.class)
@Transactional
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberDao memberDao;

    /**
     * @param telephone
     * @return
     */
    @Override
    public Member findMemberByPhone(String telephone) {
        return memberDao.findMemberByPhone(telephone);
    }

    @Override
    public void addMember(Member member) {
        if (member.getPassword() != null) {
            member.setPassword(MD5Utils.md5(member.getPassword()));
        }
        memberDao.addMember(member);
    }

    /**
     * 统计每一个月的会员数量
     *
     * @param months
     * @return
     */
    @Override
    public List<Integer> finMemberCount(List<String> months) {
        ArrayList<Integer> list = new ArrayList<>();
        for (String month : months) {
            month = month + ".31";
            Integer num = memberDao.finMemberCount(month);
            list.add(num);
        }
        return list;
    }

    /**
     * 查找所有会员信息
     *
     * @return
     */
    @Override
    public List<Map<String, Object>> findMemberData() {
        List<Map<String, Object>> memberDatas = memberDao.findMemberData();

        //        2.获取会员性别
        for (Map map : memberDatas) {
            String name = (String) map.get("name");
            if (name == null) {
                map.put("name", "性别未填");
            } else if (name.equals("1")) {
                map.put("name", "男");
            } else if (name.equals("2")) {
                map.put("name", "女");
            }
        }
        return memberDatas;
    }

    /**
     * 查找会员年龄
     * @return
     */
 //for (Map<String, Object> map : memberAgeData) {
 //       if (map.containsKey("0~18")){
 //           memberAgeData.add((Map<String, Object>) map.put("name","0~18" ));
 //       }else if (map.containsKey("18~30")){
 //           memberAgeData.add((Map<String, Object>) map.put("name","18~30" ));
 //       }else if (map.containsKey("30~45")){
 //           memberAgeData.add((Map<String, Object>) map.put("name","30~45" ));
 //       }else if (map.containsKey("45以上")){
 //           memberAgeData.add((Map<String, Object>) map.put("name","45以上" ));
 //       }
    @Override
    public List<Map<String, Object>> findMemberAge() throws Exception {
        List<Map<String, Object>> memberAgeData = memberDao.findMemberAge();
        return memberAgeData;
    }

}

